/**
 * @author 徐楠
 * @date 2022/4/24 14:38
 * @version 1.0
 */

package com.xunan.likou;

import java.util.*;

public class TheFirstUniqueCharacterInTheString {
    public static void main(String[] args) {
        String s = "leetcode";
        int i = firstUniqChar(s);
        System.out.println(i);
    }

    public static int firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        for (char aChar : chars) {
            map.put(aChar, map.getOrDefault(aChar, 0) + 1);
        }
        for (int i = 0; i < s.length(); ++i) {
            if (map.get(s.charAt(i)) == 1) {
                return i;
            }
        }
        return -1;
    }
}
